package com.ding.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ding.eduservice.entity.EduCourse;
import com.ding.eduservice.entity.EduCourseDescription;
import com.ding.eduservice.entity.EduVideo;
import com.ding.eduservice.entity.vo.Coursequery;
import com.ding.eduservice.entity.vo.PublicCourseQuery;
import com.ding.eduservice.mapper.EduCourseMapper;
import com.ding.eduservice.service.EduChapterService;
import com.ding.eduservice.service.EduCourseDescriptionService;
import com.ding.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ding.eduservice.service.EduVideoService;
import com.ding.servicebase.exceptionmanage.Dingexception;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduCourseDescriptionService service;

    @Override
    public String saveCourseInfo(Coursequery coursequery) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(coursequery,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert <= 0){
            throw new Dingexception(20001,"课程添加失败");
        }

        String id = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(coursequery.getDescription());
        eduCourseDescription.setId(id);
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    @Override
    public Coursequery getCourseInfoById(String courseId) {
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        Coursequery coursequery = new Coursequery();
        BeanUtils.copyProperties(eduCourse,coursequery);

        //查询描述表
        EduCourseDescription byId = eduCourseDescriptionService.getById(courseId);
        coursequery.setDescription(byId.getDescription());
        return coursequery;
    }

    @Override
    public void updateCourseInfo(Coursequery coursequery) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(coursequery,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if(i==0){
            throw new Dingexception(20001,"课程修改失败");
        }
        //修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(coursequery.getId());
        eduCourseDescription.setDescription(coursequery.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public PublicCourseQuery getPublicByCourseId(String courseId) {
        PublicCourseQuery publicByCourseId = baseMapper.getPublicByCourseId(courseId);
        return publicByCourseId;
    }

    @Override
    public int delCourseById(String courseId) {
        eduVideoService.delvideoByCourseId(courseId);
        eduChapterService.delchapterByCourseId(courseId);
        service.delDescriptionByCourseId(courseId);
        int i = baseMapper.deleteById(courseId);
        return i;
    }


}
